//
//  XMMyMeetingCell.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/23.
//

#import "XMMyMeetingCell.h"

@interface XMMyMeetingCell ()

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *scanNumLabel;
@property (nonatomic, strong) UIButton *editBtn; //编辑
@property (nonatomic, strong) UILabel *stateLabel;  //状态
@property (nonatomic, strong) UIButton *deleteBtn;  //删除
@property (nonatomic, strong) UIButton *rePutawayBtn; //重新上架
@property (nonatomic, strong) UIButton *soldOutBtn;  //下架

@end

@implementation XMMyMeetingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.avatarImgView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.priceLabel];
    [self addSubview:self.scanNumLabel];
    [self.contentView addSubview:self.deleteBtn];
    [self.contentView addSubview:self.rePutawayBtn];
    [self.contentView addSubview:self.editBtn];
    [self.contentView addSubview:self.soldOutBtn];
}

- (void)setContentWithOrderModel:(XMMeetingModel *)orderModel
                            page:(NSInteger)page
                       orderType:(NSInteger)orderType
                          isSelf:(BOOL)isSelf {
    if (isSelf) {
        if (orderType == 0) {
            _rePutawayBtn.hidden = YES;
            _deleteBtn.hidden = YES;
            _soldOutBtn.hidden = NO;
            _editBtn.x = _rePutawayBtn.x;
        }
        else {
            _rePutawayBtn.hidden = NO;
            _deleteBtn.hidden = NO;
            _soldOutBtn.hidden = YES;
            _editBtn.x = _soldOutBtn.x;
        }
    }
    else {
        _rePutawayBtn.hidden = YES;
        _deleteBtn.hidden = YES;
        _soldOutBtn.hidden = YES;
        _editBtn.hidden = YES;
    }
//    _avatarImgView.image = [UIImage imageWithColor:kColorHEX(0xff00ff)];
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:orderModel.coverImageUrl]];
    CGFloat nameHeight = [orderModel.title getHeightWithFont:KScaleWidth(16.0) withWidth:_nameLabel.width] + KScaleWidth(10.0);
    _nameLabel.height = MIN(nameHeight, KScaleWidth(54.0));
    _nameLabel.text = orderModel.title;//@"摩托估值选车指导交流这是产品名称这是产品名称";
    _priceLabel.text = [NSString stringWithFormat:@"¥%@", orderModel.price];//@"¥99.00";
    _scanNumLabel.text = [NSString stringWithFormat:@"曝光 %@  浏览 %@", orderModel.baogNum, orderModel.showNum];//@"曝光 123  浏览 13";
}

//编辑
- (void)editAction:(UIButton *)button {
    self.editBlock ? self.editBlock() : nil;
}

//删除
- (void)deleteAction:(UIButton *)button {
    self.deleteBlock ? self.deleteBlock() : nil;
}

//重新上架
- (void)rePutawayAction:(UIButton *)button {
    self.rePutawayBlock ? self.rePutawayBlock() : nil;
}

//下架
- (void)soldOutAction:(UIButton *)button {
    self.soldOutBlock ? self.soldOutBlock() : nil;
}

#pragma mark - Init
- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(80.0), KScaleWidth(80.0))];
        [_avatarImgView setCornerRadius:KScaleWidth(8.0)];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(102.0), KScaleWidth(18.0), kScreenWidth - KScaleWidth(102.0) - KScaleWidth(16.0), KScaleWidth(54.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.x, self.avatarImgView.maxY - KScaleWidth(30.0) - KScaleWidth(5.0), _nameLabel.width, KScaleWidth(30.0)) font:KFontSize(18.0) textColor: kColorHEX(0x333333)];
    }
    return _priceLabel;
}

- (UILabel *)scanNumLabel {
    if (!_scanNumLabel) {
        _scanNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.width/2 + self.nameLabel.x, self.priceLabel.y, self.nameLabel.width/2, _priceLabel.height) font:KFontSize(14.0) textColor:kColorHEX(0x666666) textAlignment:NSTextAlignmentRight];
    }
    return _scanNumLabel;
}

- (UIButton *)soldOutBtn {
    if (!_soldOutBtn) {
        _soldOutBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(14.0) frame:CGRectMake(kScreenWidth - KScaleWidth(115.0) - KScaleWidth(90.0), KScaleWidth(170.0) - KScaleWidth(17.0) - KScaleWidth(38.0), KScaleWidth(90.0), KScaleWidth(38.0)) title:@"下架" titleColor:kColorHEX(0x333333)];
        [_soldOutBtn setRadius:_soldOutBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
        [_soldOutBtn addTarget:self action:@selector(soldOutAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _soldOutBtn;
}

- (UIButton *)editBtn {
    if (!_editBtn) {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(14.0) frame:CGRectMake(self.soldOutBtn.maxX + KScaleWidth(10.0), _soldOutBtn.y, KScaleWidth(90.0), _soldOutBtn.height) title:@"编辑" titleColor:kColorHEX(0x333333)];
        [_editBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
        [_editBtn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
        [_editBtn setCornerRadius:_editBtn.height/2];
    }
    return _editBtn;
}

- (UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(kScreenWidth - KScaleWidth(214.0) - KScaleWidth(90.0), self.editBtn.y, KScaleWidth(90.0), KScaleWidth(38.0)) title:@"删除" titleColor:kColorHEX(0x979797)];
        [_deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
        [_deleteBtn setRadius:_deleteBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    }
    return _deleteBtn;
}

- (UIButton *)rePutawayBtn {
    if (!_rePutawayBtn) {
        _rePutawayBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(14.0) frame:CGRectMake(kScreenWidth - KScaleWidth(16.0) - KScaleWidth(90.0), self.deleteBtn.y, KScaleWidth(90.0), _deleteBtn.height) title:@"重新上架" titleColor:kColorHEX(0x333333)];
        [_rePutawayBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
        [_rePutawayBtn setCornerRadius:_rePutawayBtn.height/2];
        [_rePutawayBtn addTarget:self action:@selector(rePutawayAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rePutawayBtn;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
